#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=220;

int max(int a, int b) {
  return a>b ? a : b;
}

bool isFlat(int sum[][maxn+1], int x1, int y1, int x2, int y2) {
  int t = sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
  return t==(x2-x1+1)*(y2-y1+1) || !t;
}

int main() {
  freopen("land.in", "r", stdin);
  freopen("land.out", "w", stdout);

  static int f[2][maxn+1][maxn+1][maxn+1], g[2][maxn+1][maxn+1][maxn+1], sum[maxn+1][maxn+1];
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      char c;
      scanf(" %c ", &c);
      sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1] + (c=='#');
    }
  }

  for (int i=n; i>0; i--) {
    for (int j=m; j>0; j--) {
      for (int k=i; k<=n; k++) {
        if (isFlat(sum, i, j, k, j)) {
          if (j<m && isFlat(sum, i, j, k, f[0][i][j+1][k])) {
            f[0][i][j][k] = f[0][i][j+1][k];
          } else {
            f[0][i][j][k] = j;
          }
        } else {
          f[0][i][j][k] = j-1;
        }
      }

      for (int k=j; k<=m; k++) {
        if (isFlat(sum, i, j, i, k)) {
          if (i<n && isFlat(sum, i, j, g[0][i+1][j][k], k)) {
            g[0][i][j][k] = g[0][i+1][j][k];
          } else {
            g[0][i][j][k] = i;
          }
        } else {
          g[0][i][j][k] = i-1;
        }
      }
    }
  }

  if (f[0][1][1][n]==m) {
    printf("0");
    return 0;
  }
  for (int v=1; ; v++) {
    int tv=v&1, lv=!tv;
    for (int i=1; i<=n; i++) {
      for (int j=1; j<=m; j++) {
        for (int k=i; k<=n; k++) {
          if (f[lv][i][j][k]<m) {
            f[tv][i][j][k] = f[lv][i][f[lv][i][j][k]+1][k];
          } else {
            f[tv][i][j][k] = m;
          }
        }
        for (int k=j; k<=m; k++) {
          if (g[lv][i][j][k]<n) {
            g[tv][i][j][k] = g[lv][g[lv][i][j][k]+1][j][k];
          } else {
            g[tv][i][j][k] = n;
          }
        }
        for (int k=i; k<=n; k++) {
          int t=f[tv][i][j][k];
          if (t>=j) {
            g[tv][i][j][t] = max(g[tv][i][j][t], k);
          }
        }
        for (int k=m-1; k>=j; k--) {
          g[tv][i][j][k] = max(g[tv][i][j][k], g[tv][i][j][k+1]);
        }
        for (int k=j; k<=m; k++) {
          int t=g[tv][i][j][k];
          if(t>=i) {
            f[tv][i][j][t] = max(f[tv][i][j][t], k);
          }
        }
        for (int k=n-1; k>=i; k--) {
          f[tv][i][j][k] = max(f[tv][i][j][k], f[tv][i][j][k+1]);
        }
      }
    }
    if (f[tv][1][1][n]==m) {
      printf("%d", v);
      break;
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
